package com.hrzyit.whistle.cases.presenter

import android.util.Log
import com.hrzyit.base.ext.execute
import com.hrzyit.base.presenter.BasePresenter
import com.hrzyit.base.rx.BaseObserver
import com.hrzyit.whistle.cases.common.CaseConstant
import com.hrzyit.whistle.cases.data.bean.*
import com.hrzyit.whistle.cases.presenter.view.RegisterCaseView
import com.hrzyit.whistle.cases.service.ICaseCenterService
import com.hrzyit.whistle.cases.service.ICaseTypeService
import com.hrzyit.whistle.cases.service.IUploadService
import javax.inject.Inject

/**
 * 新建案件
 * @author YangWei
 * @date 2019/3/26
 */
class RegisterCasePresenter @Inject constructor() : BasePresenter<RegisterCaseView>() {


    @Inject
    lateinit var uploadService: IUploadService

    @Inject
    lateinit var caseTypeService: ICaseTypeService

    @Inject
    lateinit var caseCenterService: ICaseCenterService

    /**
     * 新建案件
     */
    fun caseApply(entity: CaseInfoEntity) {
        if (!checkNetWork()) {
            return
        }

        mView.showLoading()

        caseCenterService.caseApply(entity)
            .execute(object : BaseObserver<MutableMap<String,Any>>(mView) {
                override fun onNext(t: MutableMap<String,Any>) {
                    mView.onCaseApplyResult(t)
                }
            }, lifecycleProvider)
    }

    /**
     * 上传图片
     * @author YangWei
     * @date 2019/3/31
     * @param photo 本地图片文件路径
     * @return Observable<UploadInfo>   FastDfs 文件路径对象 {@link UploadInfo}
     */
    fun uploadPhoto(photo: String) {

        if (!checkNetWork()) {
            return
        }

        // 显示进度框 ??
        mView.showLoading()

        uploadService.uploadPhoto(photo)
            .execute(object : BaseObserver<UploadInfo>(mView) {

                override fun onNext(t: UploadInfo) {
                    mView.onUploadResult(t)
                }

            }, lifecycleProvider)

    }

    /**
     * 获取案件类型列表：问题类型(事件、部件、环境)
     * @author YangWei
     * @date 2019/3/25
     * @param parentId      字典项父id
     * @param id            字典项id
     * @return List<SysDictEntity> 案件类型：问题类型
     */
    fun getCaseQuestionType() {

        if (!checkNetWork()) {
            return
        }

        // 显示进度框 ??
        mView.showLoading()

        val parentId = CaseConstant.CASE_QUESTION_TYPE_ID
        val id: Int? = null

        caseTypeService.getCaseQuestionTypeList(parentId, id)
            .execute(object : BaseObserver<MutableList<SysDictEntity>>(mView) {

                override fun onNext(t: MutableList<SysDictEntity>) {
                    mView.onGetCaseQuestionTypeResult(t)
                }

            }, lifecycleProvider)
    }

    /**
     * 获取案件类型列表：大类
     * @author YangWei
     * @date 2019/3/25
     * @param parentId      案件类型父id
     * @return List<CategoryEntity> 案件类型：大类
     */
    fun getOneCategory(parentId: Long) {

        if (!checkNetWork()) {
            return
        }

        // 显示进度框 ??
        mView.showLoading()

        caseTypeService.getCategoryList(parentId, 1)
            .execute(object : BaseObserver<MutableList<CategoryEntity>>(mView) {

                override fun onNext(t: MutableList<CategoryEntity>) {
                    Log.e("大类:CategoryEntity", "${t.size}")

                    mView.onGetOneCategoryResult(t)
                }

            }, lifecycleProvider)
    }

    /**
     * 获取案件类型列表：小类
     * @author YangWei
     * @date 2019/3/25
     * @param parentId      案件类型父id
     * @return List<CategoryEntity> 案件类型：小类
     */
    fun getSecondCategory(parentId: Long) {

        if (!checkNetWork()) {
            return
        }

        // 显示进度框
        mView.showLoading()

        caseTypeService.getCategoryList(parentId, 2)
            .execute(object : BaseObserver<MutableList<CategoryEntity>>(mView) {

                override fun onNext(t: MutableList<CategoryEntity>) {
                    Log.e("小类:CategoryEntity", "${t.size}")

                    mView.onGetSecondCategoryResult(t)
                }

            }, lifecycleProvider)
    }

    /**
     * 获取案件立案条件列表
     * @author YangWei
     * @date 2019/4/7
     * @param parentId      案件类型小类id
     * @return List<CategoryDetailsEntity> 案件立案条件列表
     */
    fun getCaseConditionList(parentId: Long) {
        if (!checkNetWork()) {
            return
        }
        // 显示进度条
        mView.showLoading()

        caseTypeService.getCaseConditionList(parentId)
            .execute(object : BaseObserver<MutableList<CategoryDetailsEntity>>(mView) {
                override fun onNext(t: MutableList<CategoryDetailsEntity>) {
                    mView.onGetCaseConditionListResult(t)
                }
            }, lifecycleProvider)
    }
}